$(function (){

    // 用户登录验证
    var usernameVerify = false;
    var passwordVerify = false;
    $(".body_div").css("height",$(window).height()*0.99);

    //用户名格式校验
    $("#username").blur(function (){
        var myreg = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
        var username = $("#username").val();
        var result = myreg.test(username);
        if (!result&&username.length!=0)
            layer.tips("不符合邮箱格式","#username");
        else
            usernameVerify = true;

    })

    //密码长度校验
    $("#password").blur(function (){
        var password = $("#password").val()
        if(password.length<8&&password.length!=0)
            layer.tips("密码长度不够","#password");
        else
            passwordVerify = true;
    })


    //AJAX用户名密码正确与否校验
    $("#loginsubmit").click(function (){
        var username = $("#username").val();
        var password = $("#password").val();
        var identity = $("#identity>input:checked").val();
        console.log("按钮被点击");

        if(usernameVerify&&passwordVerify){
            console.log("提交AJAX请求");
            $.ajax({
                async: false,
                url:"/loginVerifyServlet",
                type:"post",
                data:{"username":username,"password":password,"identity":identity},
                
                success:function (data){
                    console.log("AJAX请求成功");
                    var rdata = data;
                    console.log("1.登录验证AJXA请求返回的值为："+rdata+"，值的类型为"+typeof rdata);
                    console.log("2.外部比较结果为"+(rdata==="true"));
                    
                    if(rdata=="true"){
                        // alert("正确，提交表单");
                        var ii = layer.load();
                        setTimeout(function(){
                            layer.close(ii);
                            $("#login_form").submit();
                        }, 500);
                    }
                    else{
                        // alert("失败，不提交");
                        var ii = layer.load();
                        setTimeout(function(){
                            layer.close(ii);
                            layer.tips('用户名或密码错误','#password');
                        }, 300);
                    }
                },
                dataType:"text"
            })
        }
        else{
            var ii = layer.load();
            setTimeout(function(){
                layer.close(ii);
                layer.tips('用户名或密码错误','#password');
            }, 300);
        }
    })

    // 用户登录与用户注册的切换
    $("#registeredmain").slideToggle("normal");//注册关掉
    //切换注册页面
    $("#registered_button").click(function(){
        $("#loginmain").slideToggle("normal");//登录关掉
        setTimeout(function(){
            $("#registeredmain").slideToggle("normal");//注册打开
        },250);
    })
    //切换登录页面
    $("#login_button").click(function(){
        $("#registeredmain").slideToggle("normal");//注册页面关掉
        setTimeout(function(){
            $("#loginmain").slideToggle("normal");//登录打开
        },250)
    })


    // 用户注册验证，用户名存在验证
    var registered_usernameVerify = false;
    var registered_password1Verfy = false;
    var registered_password2Verfy = false;

    //用户名输入框失去焦点
    $("#registered_username").blur(function(){
        var myreg = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
        var registered_username = $("#registered_username").val();
        var result = myreg.test(registered_username);
        if (!result&&registered_username.length!=0)
            layer.tips("不符合邮箱格式","#registered_username");
        else{
            $.post(
                "/usernameExistServlet",
                {"username":registered_username},
                function(data){
                    if(data=="true")
                        registered_usernameVerify = true;
                    else{
                        registered_usernameVerify = false;
                        $("#registered_username").css("text-decoration","line-through")
                        layer.tips("该用户名已经被注册","#registered_username");
                    }
                },"text"
            )
        }
            registered_usernameVerify = true;
    })
    //用户名输入框获得焦点
    $("#registered_username").focus(function (){
        $("#registered_username").css("text-decoration","")
    })

    $("#registered_password1").blur(function(){
        var myreg = /^[a-zA-Z-]+/
        var registered_password1 = $("#registered_password1").val();
        var result = myreg.test(registered_password1);
        if(registered_password1.length<8)
            layer.tips("密码至少有8位","#registered_password1");
        else if(!result)
            layer.tips("密码由数字和字母组成，字母开头","#registered_password1")
        else
            registered_password1Verfy = true;
    })

    $("#registered_password2").blur(function(){
        var registered_password2 = $("#registered_password2").val();
        if($("#registered_password1").val()!=registered_password2)
            layer.tips("两次密码不一致","#registered_password2")
        else
        registered_password2Verfy = true;
    })

    $("#registered_submit").click(function(){
        $("#registered_form").submit(function(){
            if (!registered_usernameVerify){
                layer.tips("用户名不可用，请重新输入","#registered_username");
                return false;
            }
            else if (!(registered_password1Verfy&&registered_password2Verfy)){
                layer.tips("密码不合法或密码不一致，请重新输入","#registered_password2");
                return false;
            }
            else
                return true;
        })
    })
    
})

